<?php
/**
 * Created by PhpStorm.
 * User: xh
 * Date: 2019/11/22
 * Time: 5:26 PM
 */

namespace weChat\model;
use think\Model;

class WeChatAuth extends Model
{
    /** 记录微信数据
     * 1.api-Login-register 调用
     * @param string $authKey
     * @param string $userInfo
     * @param string $identity
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function record(&$authKey='',$userInfo='',$identity="M"){
        $userInfoArr        = JsonDe($userInfo);
        $map = ['openid'=>$userInfoArr['openId'],'identity'=>$identity,'is_delete'=>0];
        $info = $this->where($map)->find();
        if($info){
            $info->info =  Base64En($userInfo);
            $authKey = $info->auth_key;
            return $this->save();
        }else{
            $authKey = CreateMarkup();
            $this->info         = Base64En($userInfo);
            $this->openid       = $userInfoArr['openId'];
            $this->union_id     = isset($userInfoArr['unionId'])?$userInfoArr['unionId']:'';
            $this->auth_key     = $authKey;
            $this->identity     = $identity;
            return $this->save();
        }
    }
}